import { d_sort, obj_str, cline, uDb } from 'sui';

const get_tp = (v) => {
    const ret = {
        "C": "testcage",
        "S": "testseal",
    };

    return ret[v];
};

const get_tp_mod = (v) => {
    const ret = {
        "C": "cage",
        "S": "seal",
    };

    return ret[v];
};

const load_supply = (setMain) => {
    uDb(null, "file/list/supply", {line: cline()},
        t => {
            const ret = {};
            t.forEach(d => ret[d.sn] = d.name);

            setMain(ret);
        },
        e => setMain(null));
};

const load_stuff = (setMain) => {
    uDb(null, "file/list/stuff", { line: cline() },
        t => {
            const ret = {};
            t.forEach(d => ret[d.sn] = d.name);

            setMain(ret);
        },
        e => setMain(null));
};

const reload = (hDone, val, setMain) => {
    const mod = get_tp(val);
    if (!mod) {
        return;
    }

    uDb(hDone, `file/list/${mod}`,
        {
            line: cline(),
        },
        r => {
            const ret = [];

            r.forEach(d => {
                if (obj_str(d, "state") === "close") {
                    return;
                }

                const cmd = {
                    ...d,
                    tmpl: val.toLowerCase(),
                };

                ret.push(cmd);
            });

            d_sort(ret, "tech");

            setMain(ret);
        },
        e => setMain(null));
};

const load_ng = (setMain) => {
    uDb(null, "file/list/ng", { line: cline() },
        t => {
            const ret = {};
            t.forEach(d => ret[d.sn] = d.name);

            setMain(ret);
        },
        e => setMain(null));
};

export {get_tp_mod, reload, load_ng, load_stuff, get_tp, load_supply};
